package org.fast.develop.logicflow.springboot.configuration;

import org.fast.develop.logicflow.runtime.executor.TaskListenerCreator;
import org.fast.develop.logicflow.runtime.task.delegate.TaskListener;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;

/**
 * @author 莫寅
 * @createTime 2022-11-28 04:18
 * @desc：
 */
public class SpringTaskListenerCreator implements TaskListenerCreator {

    private ApplicationContext applicationContext;

    public SpringTaskListenerCreator(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Override
    public TaskListener get(Class<TaskListener> taskListenerClass) {
        TaskListener taskListener = null;
        try {
            taskListener = applicationContext.getBean(taskListenerClass);
        }catch (NoSuchBeanDefinitionException e){
            try {
                taskListener = taskListenerClass.newInstance();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            }
        }

        return taskListener;
    }

}
